package coom.zwj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import coom.zwj.reggie.dto.DishDto;
import coom.zwj.reggie.entity.Dish;
import coom.zwj.reggie.entity.DishFlavor;
import coom.zwj.reggie.mapper.DishMapper;
import coom.zwj.reggie.service.DishFlavorService;
import coom.zwj.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 新增菜品并保存口味
     *
     * @param dishDto
     */
    //启动类不需要额外配置@EnableTransactionManagement注解,因为SpringBoot的starter已经默认开启
    @Transactional
    @Override
    public void saveWithFlavor(DishDto dishDto) {
        log.info("saveWithFlavor");
        //先保存菜品表信息
        this.save(dishDto);
        //获取当前菜品id
        Long id = dishDto.getId();
        //获取当前菜品口味列表
        List<DishFlavor> flavors = dishDto.getFlavors();
        //给列表成员的菜品id属性都赋值为当前菜品id
        flavors = flavors.stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //保存到口味表
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id获取菜品及口味
     *
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        //先查询出dish中该id信息并将其信息拷贝到dishDto中
        BeanUtils.copyProperties(dish, dishDto);
        //再查询出DishFlavor中该id的相关信息
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
        //封装到dishDto中
        dishDto.setFlavors(list);
        return dishDto;
    }

    /**
     * 修改菜品及其口味
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //先保存基本信息到dish表
        this.updateById(dishDto);
        Long id = dishDto.getId();
        //删除该菜品所属口味
        LambdaQueryWrapper<DishFlavor> dishFlavorLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishFlavorLambdaQueryWrapper.eq(DishFlavor::getDishId, id);
        dishFlavorService.remove(dishFlavorLambdaQueryWrapper);
        //遍历所有口味为其赋值当前菜品id
        List<DishFlavor> collect = dishDto.getFlavors().stream().map((item) -> {
            item.setDishId(id);
            return item;
        }).collect(Collectors.toList());
        //保存当前菜品的口味到DishFlavor表中
        dishFlavorService.saveBatch(collect);
    }
}
